<?php

namespace Home\Controller;

class DatumController extends AuthController{

    protected $table;
    protected $tag;
    protected $type;
    protected $list;

    public function __construct(){
        parent::__construct();

        $this->table=D('file');
        $this->tag=D('file_tag');
        $this->type='90'; //代表文件类型
        $this->list ='10'; //一页显示几条

    }



    public function index(){

        $total =$this->table->count();


        $page =new \Common\Common\Page($total,$this->list);

        $show =$page->show();


		$this->assign('allFile',$this->table->all_file($this->list));

		$this->assign('show',$show);
		$this->assign('category',M(''));
        $this->display();

    }

    public function details(){

        if(IS_GET){
            $this->assign('file',$this->table->once_file(I('get.id')));
        }


        $this->display();
    }


    public function upload(){


        if (IS_POST) {

            $success = false;
            $post = I('post.');
            $file_url = explode('/', $post['file']);

            $post['file_img_url'] = $this->copy_datum($post['img'], 'images');
            $post['file_url'] = $this->copy_datum($post['file'], 'files');
            $file_parameter = $this->memcache(array_pop($file_url));
            $post['file_type'] = $file_parameter['type'];
            $post['file_size'] = $file_parameter['size'];
            $post['file_tag_type'] = $this->type;
            $post['user_id'] =session('home_user');
            if ($this->table->create($post)) {

                if ($file_id = $this->table->add()) {
                    $success = true;
                } else {

                    $this->ajaxReturn(array('error' => '上传失败'));
                }

            } else {
                $this->ajaxReturn(array('error' => $this->table->getError()));
            }

        if(is_array($post['tag'])){
            foreach ($post['tag'] as $key => $val) {
                $data = array();
                $data['tag_name'] = $val;
                $data['tag_type'] = $this->type;
                $data['file_id'] = $file_id;
                $data['create_time'] = time();
                if ($this->tag->create($data)) {
                    if ($this->tag->add($data)) {

                        $success = true;
                    } else {
                        $this->ajaxReturn(array('error' => '上传失败'));
                    }

                } else {
                    $this->ajaxReturn(array('error' => $this->tag->getError()));

                }

            }
        }

            if ($success) {

                $this->ajaxReturn(array('success' => '上传成功页面跳转中', 'url' => U('Index/index')));
            } else {
                $this->ajaxReturn(array('error' => '上传失败'));
            }

        }


        $this->assign('category', M('category')->where('category_type=%d', $this->type)->select());
        $this->display();
    }

    public function download(){
        $public ='./Public/';

        if (IS_GET) {

            $file=$this->table->once_file(I('get.id'));

            $file['file_url'] ='./Public/'.$file['file_url'];
            if (file_exists(iconv('utf-8', 'gbk',$file['file_url']))) {

                $down = new \Common\Common\Download();

                $down->load($file);
            }else{
                die('文件不存在请联系我们');
            }
        }
    }

    public function datum_file(){

        $size = 10*1024*1024;

        $config =array(
            'maxSize' =>$size,
            'exts' =>array ('rar', 'zip'),
        );

       return $this->file($config);

    }


    function copy_datum($url,$type){

        if(!empty($url)){
            $public ='./Public/';
            $url_dir =$public.$url;
            if(file_exists($url_dir)){
                $file =explode('/',$url);
                $file_name =array_pop($file);
                $new_file = './Uploads/'.date('Y-m-d').'/'.$type.'/';

                $dir =$public.$new_file;

                $file_url =$dir.$file_name;

                if(!is_dir($dir)){
                   mkdir(iconv('UTF-8','GBK',$dir),0777,true);
                }
                copy($url_dir,$file_url);
                $new_url =$new_file.$file_name;
                return  $new_url;
            }
        }
        return;
    }



    public function img(){
        $size = 2*1024*1024;

        $config =array(
            'maxSize' =>$size,
            'exts' =>array ('jpg', 'gif', 'png', 'jpeg','bmp'),
        );
       return $this->file($config);
    }

    public  function  file($config){

        $upload = new \Think\Upload($config);

        $upload->rootPath ='./Public/';

       $upload->savePath ='./upload_Runtime/'.date('Y-m-d').'/';

        $file = $upload->upload();

       if(!$file){

           $this->ajaxReturn($upload->getError());
       }else{
           foreach ($file as $val){

               $this->memcache($val['savename'],$val);

               $this->ajaxReturn(  array('url'=>$val['savepath'].$val['savename']));

           }
          $this->ajaxReturn($file);
       }

       return;
    }





}